package pub.wkq.api;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Slf4j
@Component
@Aspect
public class ServiceLogAspect {
    /**
     * 切面表达式
     */
    @Around("execution(* pub.wkq.*.service.impl..*.*(..))")
    public Object recordTimeLog(ProceedingJoinPoint joinPoint) throws Throwable {
//        StopWatch stopWatch = new StopWatch();//spring自带的计时器
        long begin = System.currentTimeMillis();//这种方式获取的时间不准确，因为可能会有多个线程同时执行
//        stopWatch.start( joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName());
        log.info("===== 开始执行 {}.{} =====", joinPoint.getTarget().getClass(), joinPoint.getSignature().getName());
        Object proceed = joinPoint.proceed();
//        stopWatch.stop();
//        log.info(stopWatch.prettyPrint());
//        log.info(stopWatch.shortSummary());
//        log.info(stopWatch.getTotalTimeMillis() + "");
//        log.info(stopWatch.getTotalTimeSeconds() + "");
        long end = System.currentTimeMillis();
        long takeTime = end - begin;
        if (takeTime > 3000) {
            log.error("===== 执行结束，耗时：{} 毫秒 =====", takeTime);
        } else if (takeTime > 2000) {
            log.warn("===== 执行结束，耗时：{} 毫秒 =====", takeTime);
        } else {
            log.info("===== 执行结束，耗时：{} 毫秒 =====", takeTime);
        }
        return proceed;
    }
}
